package com.eastfair.pay.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.eastfair.annotation.annotation.log.SysLog;
import com.eastfair.annotation.annotation.security.PreAuth;
import com.eastfair.boot.controller.SuperController;
import com.eastfair.boot.request.PageParams;
import com.eastfair.core.base.R;
import com.eastfair.core.exception.BizException;
import com.eastfair.echo.core.EchoService;
import com.eastfair.pay.dto.TransactionDTO;
import com.eastfair.pay.dto.TransactionPageQuery;
import com.eastfair.pay.dto.TransactionSaveDTO;
import com.eastfair.pay.dto.TransactionUpdateDTO;
import com.eastfair.pay.entity.Transaction;
import com.eastfair.pay.service.TransactionService;
import com.eastfair.pay.vo.TransactionStatisticsVO;
import com.eastfair.pay.vo.TransactionVO;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


/**
 * <p>
 * 前端控制器
 * 交易记录
 * 目前不使用了
 * </p>
 *
 * @author clm
 * @date 2023-02-13
 */
@Slf4j
@Validated
@RestController
@RequestMapping("/transaction")
@Api(value = "Transaction", tags = "交易记录")
@PreAuth(replace = "pay:transaction:")
public class TransactionController extends SuperController<TransactionService, Long, Transaction, TransactionPageQuery, TransactionSaveDTO, TransactionUpdateDTO> {

    @Autowired
    private EchoService echoService;

    @Resource
    private TransactionService transactionService;

    @Override
    public void handlerResult(IPage<Transaction> page) {
        // 想让返回值实现自动回显，请将此行代码打开
        // echoService.action(page);
    }
    /**
     * Excel导入后的操作
     *
     * @param list
     */
    @Override
    public R<Boolean> handlerImport(List<Map<String, String>> list){
        List<Transaction> transactionList = list.stream().map((map) -> {
            Transaction transaction = Transaction.builder().build();
            //TODO 请在这里完成转换
            return transaction;
        }).collect(Collectors.toList());

        return R.success(baseService.saveBatch(transactionList));
    }



    /**
     * 处理参数
     *
     * @param params 分页参数
     */
    @Override
    public void handlerQueryParams(PageParams<TransactionPageQuery> params) {
        TransactionPageQuery pageQuery = params.getModel();

    }


    /**
     * 保存交易记录
     * @param transactionDTO
     * @return
     */
    @PostMapping("/save")
    @SysLog("保存交易记录")
    public R save(@RequestBody TransactionDTO transactionDTO){
        try {
            transactionService.save(transactionDTO);
            return R.success();
        } catch(BizException biz) {
            return R.fail(biz);
        }
    }

    /**
     * 更新交易记录
     * @param transactionDTO
     * @return
     */
    @PostMapping("/update")
    @SysLog("更新交易记录")
    public R update(@RequestBody TransactionDTO transactionDTO){
        try {
            transactionService.updateById(transactionDTO);
            return R.success();
        } catch(BizException biz) {
            return R.fail(biz);
        }
    }

    /**
     * @Author clm
     * @Description //分页检索
     * @Date 17:04 2023/2/13
     * @Param [params]
     * @return com.eastfair.core.base.R<com.baomidou.mybatisplus.extension.plugins.pagination.Page<com.eastfair.pay.vo.TransactionVO>>
     **/
    @PostMapping("/queryPage")
    @SysLog("分页检索交易记录")
    public R<Page<TransactionVO>> queryPage(@RequestBody @Validated PageParams<TransactionPageQuery> params){

        try {
            return R.success(baseService.queryList(params));
        } catch(BizException biz) {
            return R.fail(biz);
        }

    }

    /**
     * @Author clm
     * @Description //交易记录 统计
     * @Date  2023/2/16
     * @Param
     * @return
     **/
    @PostMapping("/transactionStatistics")
    @SysLog("交易记录 统计")
    public R<List<TransactionStatisticsVO>> transactionStatistics(@RequestBody TransactionDTO param){

        try {
            return R.success(baseService.getTransactionStatisticsInfo(param));
        } catch(BizException biz) {
            return R.fail(biz);
        }

    }


    /**
     * @Author clm
     * @Description //通过查询条件获取交易记录
     * @Date 16:44 2023/2/20
     * @Param [transactionDTO]
     * @return com.eastfair.core.base.R<java.util.List<com.eastfair.pay.vo.TransactionVO>>
     **/
    @PostMapping("/getTransactions")
    @SysLog("通过查询条件获取交易记录")
    public R<List<TransactionVO>> getTransactions(@RequestBody TransactionDTO transactionDTO){

        try {
            return R.success(baseService.getTransactionStatistics(transactionDTO));
        } catch(BizException biz) {
            return R.fail(biz);
        }

    }
}
